package tk.mybatis.springboot.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;
import tk.mybatis.springboot.constants.RequestLimit;
import tk.mybatis.springboot.model.CarInfo;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.model.TWeixinAddress;
import tk.mybatis.springboot.service.WexinInsuranceService;
import tk.mybatis.springboot.wexin.bean.Userinfo;
import tk.mybatis.springboot.wexin.util.HttpUtil;

@Controller
public class WexinInsuranceNewController extends BaseController{
	@Autowired
	private WexinInsuranceService wexinInsuranceService;
	//获取微信用户历史车牌号
    @RequestMapping(value = "weixin/saveTruckCardNew")
    @ResponseBody
    @RequestLimit(limit=30,time=3600000)
    public Message saveTruckCardNew(HttpServletRequest request, HttpServletResponse response, String truckNo, String ownerName)throws Exception {  	
    	return wexinInsuranceService.saveTruckCardNew(request, truckNo, ownerName);
    }
    
    @RequestMapping(value = "weixin/login")
    @ResponseBody
    public Message login(HttpServletRequest request, HttpServletResponse response, String phone, String token)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.login(request, phone, token);
    	}
    	return error_msg("请重新获取授权");
    }
    
    @RequestMapping(value = "weixin/saveCoverageNew")
    @ResponseBody
    @RequestLimit(limit=30,time=3600000)
    public Message saveCoverageNew(HttpServletRequest request, HttpServletResponse response, @RequestBody  JSONObject obj)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.saveCoverageNew(request, obj);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/saveTruckInfoNew")
    @ResponseBody
    @RequestLimit(limit=30,time=3600000)
    public Message saveTruckInfoNew(HttpServletRequest request, HttpServletResponse response, CarInfo carInfo)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.saveTruckInfoNew(userinfo.getOpenid(), carInfo);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/quote/simulate")
    @ResponseBody
    @RequestLimit(limit=30,time=3600000)
    public Message quoteSimulate(HttpServletRequest request, HttpServletResponse response, String insurerCode)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.quoteSimulate(request, insurerCode);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/getCity")
    @ResponseBody
    public Message getCity(HttpServletRequest request, HttpServletResponse response, String cityName)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.getVagueCity(cityName);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/addAddress")
    @ResponseBody
    public Message addAddress(HttpServletRequest request, HttpServletResponse response, TWeixinAddress tWeixinAddress)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		tWeixinAddress.setOpenid(userinfo.getOpenid());
    		return wexinInsuranceService.addAddress(tWeixinAddress);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/delAddress")
    @ResponseBody
    public Message delAddress(HttpServletRequest request, HttpServletResponse response, String id)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.delAddress(id);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/findAddress")
    @ResponseBody
    public Message findAddress(HttpServletRequest request, HttpServletResponse response, String id)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.findAddress(id);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/getAddress")
    @ResponseBody
    public Message delAddress(HttpServletRequest request, HttpServletResponse response)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.getAddress(userinfo.getOpenid());
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/setDefault")
    @ResponseBody
    public Message setDefault(HttpServletRequest request, HttpServletResponse response, String id)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.setDefault(userinfo.getOpenid(), id);
    	}
    	return error_msg("网络异常");
    }
    
    @RequestMapping(value = "weixin/getCityAll")
    @ResponseBody
    public Message getCityAll(HttpServletRequest request, HttpServletResponse response)throws Exception {  	
    	return wexinInsuranceService.getCityAll();
    }
    
    @RequestMapping(value = "weixin/getJsonData")
    @ResponseBody
    public Message getJsonData(HttpServletRequest request, HttpServletResponse response)throws Exception {  	
    	return wexinInsuranceService.getJsonData();
    }
    
    @RequestMapping(value = "weixin/getInsuranceAll")
    @ResponseBody
    public Message getInsuranceAll(HttpServletRequest request, HttpServletResponse response, String cityCode)throws Exception {  	
    	return wexinInsuranceService.getInsuranceAll(cityCode);
    }
    
    @RequestMapping(value = "weixin/upLoadIdcord")
    @ResponseBody
    public Message upLoadIdcord(HttpServletRequest request, HttpServletResponse response, String img)throws Exception {  	
    	Map<String, String> request_obj = new HashMap<String, String>();
		request_obj.put("img", img);
		request_obj.put("type", "1");
    	String sendPost = HttpUtil.sendPost("http://m.ydche.com/baidu/imgRecognition", request_obj);
    	JSONObject jsonObject = JSONObject.fromObject(sendPost);
    	String data = jsonObject.getString("data");
    	JSONObject fromObject = JSONObject.fromObject(data);
    	return Message.success(fromObject);
    }
    
    @RequestMapping(value = "weixin/getOrder")
    @ResponseBody
    public Message getOrder(HttpServletRequest request, HttpServletResponse response, String type, Integer rows)throws Exception {  
    	HttpSession session = request.getSession();
    	String user_id = session.getAttribute("user_id").toString();
    	return wexinInsuranceService.getOrder(user_id, type, rows);
    }
    
    @RequestMapping(value = "weixin/getOrderInfo")
    @ResponseBody
    public Message getOrderInfo(HttpServletRequest request, HttpServletResponse response, String id)throws Exception {  
    	return wexinInsuranceService.getOrderInfo(id);
    }
    
    @RequestMapping(value = "weixin/delOrder")
    @ResponseBody
    public Message delOrder(HttpServletRequest request, HttpServletResponse response, Integer id)throws Exception {  
    	return wexinInsuranceService.delOrder(id);
    }
    
    @RequestMapping(value = "weixin/getCustomer")
    @ResponseBody
    public Message getCustomer(HttpServletRequest request, HttpServletResponse response, String order, Integer isValid)throws Exception {  
    	return wexinInsuranceService.getCustomer(request, order, isValid);
    }
    
    @RequestMapping(value = "weixin/upLoadVehicleLicense")
    @ResponseBody
    public Message upLoadVehicleLicense(HttpServletRequest request, HttpServletResponse response, String img)throws Exception {  	
    	Map<String, String> request_obj = new HashMap<String, String>();
		request_obj.put("img", img);
		request_obj.put("type", "3");
    	String sendPost = HttpUtil.sendPost("http://m.ydche.com/baidu/imgRecognition", request_obj);
    	JSONObject jsonObject = JSONObject.fromObject(sendPost);
    	String data = jsonObject.getString("data");
    	JSONObject fromObject = JSONObject.fromObject(data);
    	return Message.success(fromObject);
    }
}
